package com.koron.common.web.qrcode.utils;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author twcao
 * @title: QRCodeUtils
 * @projectName GDH_EAM_V2
 * @description: 二维码工具
 * @date 2022/1/2510:07
 */
@Slf4j
public class QRCodeUtils {

    /**
     * 将内容转为二维码
     * @param width 宽度
     * @param height 高度
     * @param content 内容
     * @param suffix 文件后缀
     * @return
     * @throws WriterException
     * @throws IOException
     */
    public static byte[] createQRCode(int width, int height, String content, String suffix) throws WriterException, IOException {
        // 二维码基本参数设置
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码字符集utf-8
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);// 设置纠错等级L/M/Q/H,纠错等级越高越不易识别，当前设置等级为最高等级H
        hints.put(EncodeHintType.MARGIN, 0);// 可设置范围为0-10，但仅四个变化0 1(2) 3(4 5 6) 7(8 9 10)
        // 生成图片类型为QRCode
        BarcodeFormat format = BarcodeFormat.QR_CODE;
        // 创建位矩阵对象
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, format, width, height, hints);
        // 设置位矩阵转图片的参数
        // MatrixToImageConfig config = new MatrixToImageConfig(Color.black.getRGB(), Color.white.getRGB());
        // 位矩阵对象转流对象
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, suffix, os);
        return os.toByteArray();
    }

    public static byte[] createQRCode(int width, int height, String content) throws WriterException, IOException {
        return createQRCode(width, height, content, "png");
    }

    /**
     * 从二维码中解析出内容
     * @param is
     * @return
     */
    public static String extractText(InputStream is) {
        BufferedImage image;
        try {
            image = ImageIO.read(is);
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
            return result.getText();
        } catch (IOException e) {
           log.error(e.getMessage(), e);
        } catch (NotFoundException e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }
}
